//
//  TSPictureCollectionViewCell.swift
//  Picture
//
//  Created by 王磊 on 28/04/2017.
//  Copyright © 2017 ThreeStone. All rights reserved.
//

import UIKit

class TSPictureCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        commitInitOwnProperties()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    internal var indexPath: NSIndexPath?
    
    private var comment: UIButton = UIButton(type: .Custom)
    
    private var pictureData: AnyObject?
    
    private var label: TSAttributedLabel = TSAttributedLabel()
}
extension TSPictureCollectionViewCell {
    
    internal func updateData(data: AnyObject) {
        
        self.pictureData = data
    }
}
extension TSPictureCollectionViewCell {
    
}

extension TSPictureCollectionViewCell {
    internal func commitInitOwnProperties() {
        
        backgroundColor = UIColor.greenColor()
        
        layer.borderColor = UIColor.redColor().CGColor
        
        layer.borderWidth = 1
        // 按钮点击实践
        comment.addTarget(self, action: #selector(commentTriggered), forControlEvents: .TouchUpInside)
        
        contentView.addSubview(comment)
        
        comment.backgroundColor = UIColor.redColor()
        
        contentView.addSubview(label)
        
        label.numberOfLines = 1
        
        label.setAttibuteText(NSAttributedString(string: "王磊 ", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(15),NSForegroundColorAttributeName: UIColor.redColor()]))
        
        label.appendImage(UIImage(named: "男_icon")!)
        
        label.appendAttributeText(NSAttributedString(string: " \(29)岁", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(12),NSForegroundColorAttributeName: UIColor.blackColor()]))
        
        label.backgroundColor = UIColor.clearColor()
    }
}

extension TSPictureCollectionViewCell {
    
    @objc private func commentTriggered() {
        
        if !unsureObervers!.isEmpty {
            
            let argu = TSArguInfo()
            
            argu.data = pictureData
            
            argu.view = self
            
            argu.indexPath = indexPath
            
            argu.itemType = TSItemClickType.Comment
            
            performSelWithArgu(argu)
            
        }
    }
}

extension TSPictureCollectionViewCell {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //        comment.frame = CGRectMake(0, 0, 50, 50)
        
        label.frame = CGRectMake(10, 10, bounds.size.width, 30)
        
    }
}

